//package com.plian.system.service.dp.impl;
//
//
//import com.baomidou.mybatisplus.core.toolkit.Wrappers;
//import com.plian.Tools.*;
//import com.plian.system.config.YthTaskObserver;
//import com.plian.system.constant.sys.FormTypeConstant;
//import com.plian.system.constant.yth.YthConstant;
//import com.plian.system.entity.dp.LimitPeopleEntity;
//import com.plian.system.entity.hr.staff.StaffFiles;
//import com.plian.system.entity.sys.Org;
//import com.plian.system.entity.yth.dp.LimitPersonnelReport;
//import com.plian.system.mapper.dp.LimitPeopleMapper;
//import com.plian.system.mapper.yth.dp.LimitPersonnelReportMapper;
//import com.plian.system.mybatis.base.BaseServiceImpl;
//import com.plian.system.service.dp.ILimitPeopleService;
//import com.plian.system.service.hr.staff.IStaffFilesService;
//import com.plian.system.service.sys.orgzation.IOrgService;
//import com.plian.system.vo.dp.LimitPeopleVO;
//import com.plian.system.vo.hr.staff.StaffFilesVO;
//import com.plian.system.wrapper.dp.LimitPeopleWrapper;
//import com.plian.system.wrapper.hr.staff.StaffFilesWrapper;
//import lombok.AllArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.time.ZoneOffset;
//import java.util.*;
//import java.util.function.Function;
//import java.util.stream.Collectors;
//
//import static com.plian.system.constant.sys.BaseConstant.BASE64_KEY;
//import static com.plian.system.constant.sys.FormStatusConstant.UNREVIEWED;
//
///**
// * 禁入限制人员档案 服务实现类
// *
// * @author gangan
// * @since 2023-05-23
// */
//@Slf4j
//@Service
//@AllArgsConstructor
//public class LimitPeopleServiceImpl extends BaseServiceImpl<LimitPeopleMapper, LimitPeopleEntity> implements ILimitPeopleService {
//
//    private IStaffFilesService staffFilesService;
//
//    private StaffFilesWrapper staffFilesWrapper;
//
//    private LimitPeopleWrapper limitPeopleWrapper;
//
//    private LimitPersonnelReportMapper limitPersonnelReportMapper;
//
//    private IOrgService orgService;
//
//
//    @Override
//    @Transactional(rollbackFor = RuntimeException.class)
//    public boolean saveOrUpdate(LimitPeopleEntity entity) {
//        if (entity.getId() == null) {
//            entity.setFormType(String.valueOf(FormTypeConstant.DP_LIMIT_PEOPLE));
//            entity.setStatus(UNREVIEWED);
//        }
//        return super.saveOrUpdate(entity);
//    }
//
//    @Override
//    public LimitPeopleVO getDetailVO(LimitPeopleEntity entity) {
//        LimitPeopleVO limitPeopleVO = limitPeopleWrapper.entityToVO(entity);
//        if (limitPeopleVO.getStaffId() != null){
//            StaffFilesVO staffFilesVO = staffFilesWrapper.entityToVO(staffFilesService.getById(limitPeopleVO.getStaffId()));
//            limitPeopleVO.setStaffFilesVO(staffFilesVO);
//        }
//        return limitPeopleVO;
//    }
//
//    @Override
//    @YthTaskObserver
//    public boolean ythUpdateDataByDate(String date) {
//        Date startTime = DateUtil.parse(date + DateUtil.MIN_HOUR_MINUTE_SECOND, DateUtil.PATTERN_DATETIME);
//        Date endTime = DateUtil.parse(date + DateUtil.MAX_HOUR_MINUTE_SECOND, DateUtil.PATTERN_DATETIME);
//        List<LimitPersonnelReport> limitPersonnelReportList = limitPersonnelReportMapper.selectList(Wrappers.<LimitPersonnelReport>lambdaQuery()
//                .ge(LimitPersonnelReport::getCreateTime, startTime)
//                .le(LimitPersonnelReport::getCreateTime, endTime));
//        List<LimitPeopleEntity> oldDataList = list(Wrappers.<LimitPeopleEntity>lambdaQuery().eq(LimitPeopleEntity::getDataSource, YthConstant.DATA_NINGBO));
//        Map<String, LimitPeopleEntity> oldDataMap = oldDataList.stream().collect(Collectors.toMap(LimitPeopleEntity::getDataSourceId, realProperty -> realProperty));
//        List<Org> orgList = orgService.getYthOrgList();
//        Map<String, Org> dataSourceIdOrgMap = orgList.stream().collect(Collectors.toMap(Org::getDataSourceId, Function.identity()));
//        List<StaffFiles> allStaffFiles = staffFilesService.list(Wrappers.<StaffFiles>lambdaQuery()
//                .isNotNull(StaffFiles::getDocumentNumber));
//        Map<String, List<StaffFiles>> allStaffFilesMap = allStaffFiles.stream().collect(Collectors.groupingBy(StaffFiles::getDocumentNumber));
//
//        Map<String, LimitPeopleEntity> allLimitPeopleMap = list(Wrappers.<LimitPeopleEntity>lambdaQuery()
//                .eq(LimitPeopleEntity::getDataSource, YthConstant.DATA_NINGBO)).stream()
//                .collect(Collectors.toMap(entity -> entity.getCreateCompanyId() + StringPool.DASH + entity.getStaffId()
//                , limitPeople -> limitPeople));
//        if (CollectionUtil.isNotEmpty(limitPersonnelReportList)){
//            List<LimitPeopleEntity> submitList = new ArrayList<>();
//            for (LimitPersonnelReport limitPersonnelReport : limitPersonnelReportList){
//                String key = EncryptUtil.encryptBase64(limitPersonnelReport.getDocumentNumber(), BASE64_KEY);
//                if (!allStaffFilesMap.containsKey(key)){
//                    log.info(StringUtil.format( "documentNumber={}, 该人员在系统不存在", limitPersonnelReport.getDocumentNumber()));
//                    continue;
//                }
//                if (!dataSourceIdOrgMap.containsKey(limitPersonnelReport.getReportOrgId() + "")){
//                    log.info(StringUtil.format( "reportOrgId={},reportOrgName={}, 该单位在系统不存在", limitPersonnelReport.getReportOrgId(), limitPersonnelReport.getReportOrgName()));
//                    continue;
//                }
//                Org org = dataSourceIdOrgMap.get(limitPersonnelReport.getReportOrgId() + "");
//                LimitPeopleEntity limitPeopleEntity = ythEntityToEntity(limitPersonnelReport, org);
//                if (oldDataMap.containsKey(String.valueOf(limitPersonnelReport.getId()))){
//                    LimitPeopleEntity oldData = oldDataMap.get(String.valueOf(limitPersonnelReport.getId()));
//                    limitPeopleEntity.setId(oldData.getId());
//                }
//                StaffFiles staffFiles = allStaffFilesMap.get(key).get(0);
//                String limitPeopleKey = org.getId() + StringPool.DASH + staffFiles.getId();
//                if (allLimitPeopleMap.containsKey(limitPeopleKey)){
//                    limitPeopleEntity.setId(allLimitPeopleMap.get(limitPeopleKey).getId());
//                }
//                limitPeopleEntity.setStaffId(staffFiles.getId());
//                submitList.add(limitPeopleEntity);
//            }
//            if (CollectionUtil.isNotEmpty(submitList)){
//                saveOrUpdateBatch(submitList);
//            }
//        }
//        return true;
//    }
//
//    private LimitPeopleEntity ythEntityToEntity(LimitPersonnelReport limitPersonnelReport, Org org){
//        LimitPeopleEntity limitPeopleEntity = new LimitPeopleEntity();
//        limitPeopleEntity.setLimitPeopleName(limitPersonnelReport.getLimitPerson());
//        limitPeopleEntity.setLimitReason(limitPersonnelReport.getLimitReason());
//        limitPeopleEntity.setIsEnterprisePersonnel(limitPersonnelReport.getIsEnterprisePerson());
//        limitPeopleEntity.setFormType(FormTypeConstant.DP_LIMIT_PEOPLE + "");
//        limitPeopleEntity.setDataSource(YthConstant.DATA_NINGBO);
//        limitPeopleEntity.setDataSourceId(limitPersonnelReport.getId() + "");
//        limitPeopleEntity.setCreateCompanyId(org.getId());
//        limitPeopleEntity.setLimitTime(Date.from(limitPersonnelReport.getLimitTime().atStartOfDay(ZoneOffset.UTC).toInstant()));
//        return limitPeopleEntity;
//    }
//}
